#include "tableviewmodel.h"

#include <QColor>

TableViewModel::TableViewModel(QObject *parent)
    : QAbstractTableModel(parent)
{

}

TableViewModel::~TableViewModel()
{

}

// 更新表格数据
void TableViewModel::updateData(QList<Data> recordList)
{
    m_recordList = recordList;
    beginResetModel();
    endResetModel();
}

void TableViewModel::setHorizontalHeader(const QStringList &headers)
{
    m_HorizontalHeader = headers;
}

// 行数
int TableViewModel::rowCount(const QModelIndex &parent) const
{
    return m_recordList.count();
}

// 列数
int TableViewModel::columnCount(const QModelIndex &parent) const
{
    return m_HorizontalHeader.size();
}

// 设置表格项数据
bool TableViewModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    int nColumn = index.column();
    Data record = m_recordList.at(index.row());
    switch (role)
    {
    case Qt::DisplayRole:
    {
        if (nColumn == CL_name)
        {
            record.name = value.toString();
            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);
            return true;
        }
    }
    case Qt::CheckStateRole:
    case Qt::UserRole:
    {
        if (nColumn == CL_checked)
        {
            record.bChecked = value.toBool();

            m_recordList.replace(index.row(), record);
            emit dataChanged(index, index);

            if (role == Qt::UserRole)
                onStateChanged();
            return true;
        }if(nColumn == CL_switch)
        {
            record.bSwitch = value.toBool();
            m_recordList.replace(index.row(), record);

            return true;
        }
    }
    default:
        return false;
    }
    return false;
}

// 表格项数据
QVariant TableViewModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    int nRow = index.row();
    int nColumn = index.column();
    Data record = m_recordList.at(nRow);

    switch (role)
    {
    case Qt::TextColorRole:
        return QColor(Qt::black);
    case Qt::TextAlignmentRole:
        return QVariant(Qt::AlignHCenter | Qt::AlignVCenter);
    case Qt::DisplayRole:
    {
        switch(nColumn)
        {
        case 1: return record.name;
        default:
            break;
        }

        return QVariant();
    }
    case Qt::UserRole:
    {
        if (nColumn == CL_checked)
            return record.bChecked;
        else if(nColumn == CL_switch)
            return record.bSwitch;
    }
    default:
        return QVariant();
    }

    return QVariant();
}

// 表头数据
QVariant TableViewModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    switch (role)
    {
    case Qt::TextAlignmentRole:
        return QVariant(Qt::AlignCenter | Qt::AlignVCenter);
    case Qt::DisplayRole:
    {
        if (orientation == Qt::Horizontal) {
            return m_HorizontalHeader.at(section);
        }else
        {
            return QAbstractTableModel::headerData(section, orientation, role);
        }
    }
    default:
        return QVariant();
    }

    return QVariant();
}

// 表格可选中、可复选
Qt::ItemFlags TableViewModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return QAbstractItemModel::flags(index);

    Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;

    return flags;
}


// 复选状态改变，通知表头更新复选状态
void TableViewModel::onStateChanged()
{
    Qt::CheckState state = Qt::Unchecked;
    int nCount = m_recordList.count();
    int nSelectedCount = 0;
    Data record;
    for (int i = 0; i < nCount; ++i)
    {
        record = m_recordList.at(i);
        if (record.bChecked)
            ++nSelectedCount;
    }

    if (nSelectedCount >= nCount)
    {
        state = Qt::Checked;
    }
    else if (nSelectedCount > 0)
    {
        state = Qt::PartiallyChecked;
    }

    emit stateChanged(state);
}

// 接收表头复选状态更新变化，修改表格复选状态
void TableViewModel::onStateChanged(int state)
{
    QModelIndex index;
    for (int i = 0; i < rowCount(); ++i)
    {
        index = this->index(i, CL_checked);
        setData(index, state == Qt::Checked, Qt::CheckStateRole);
    }
}

